package com.cumulus.MoveCopy;

import java.util.ArrayList;

import com.cumulus.MoveCopy.R.string;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

/*
 * Class TransitionActivity
 * Both 
 */

public class TransitionActivity extends Activity {

	private static final String TAG = "TransitionActivity";
	private Intent intent;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ArrayList<Integer> players = this.getIntent().getExtras()
				.getIntegerArrayList("player_arr");
		int current_index = this.getIntent().getExtras()
				.getInt("current_index");
		int recorder_index = this.getIntent().getExtras()
				.getInt("recorder_index");
		int percent = this.getIntent().getExtras()
				.getInt("percentage");
		boolean attempted = this.getIntent().getExtras()
				.getBoolean("attempted");
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);


		/*
		 * FUTURE: If current player index == original mover index-1 i.e. if
		 * current is last player: Go to TransitionActivity with choice between
		 * Retry, Back to player 1, *New Player*, or End Game
		 */

		/*
		 * If round has gone back to original recorder: Go to TransitionActivity
		 * with choice between Retry or Next (Goes to GameResultsActivity)
		 */
		Log.d(TAG,"c"+current_index+"r"+recorder_index);
		int ci=current_index-1;
		if( ci==-1){
			ci=players.size()-1;
		}
		if (ci == recorder_index && !attempted) {
			// Just recorded
			setContentView(R.layout.recorder_transition_activity);
			((Button)findViewById(R.id.share_button)).setVisibility(View.INVISIBLE);
			intent = new Intent(TransitionActivity.this, ReplyActivity.class);
		} else {
			
			setContentView(R.layout.transition_activity);
			Log.d(TAG,"c"+current_index+"dg   "+attempted);
			int c=current_index;
			if( c==0){
				c=players.size();
			}					
			((TextView)findViewById(R.id.accuracy_text)).setText("Player "+((c))+"'s Accuracy: " + percent + "%" );
			((Button)findViewById(R.id.new_player_button)).setVisibility(View.INVISIBLE);
			if (ci == recorder_index) {
				// Recorder attempts their motion
				//Intent 
				//turn on some buttons
				((TextView)findViewById(R.id.next_player_button)).setText(string.next_button);
				((Button)findViewById(R.id.next_player_button)).setOnClickListener(scoreBoard);
				
			} else {
				int min=recorder_index-1;
				if (min==-1){
					min=players.size()-1;
				}		
				intent = new Intent(TransitionActivity.this, ReplyActivity.class);
				if (ci==min)
				{
					Log.d(TAG,"player -1");
					((Button)findViewById(R.id.new_player_button)).setVisibility(View.VISIBLE);
				//	((Button)findViewById(R.id.new_player_button)).setOnClickListener(addNewPlayer);				
				}
				else{
					Log.d(TAG,"c"+current_index+1+"r"+recorder_index);
				}
			}
		}

	}

	/*
	 * startReplyActivity Get number_players, player ArrayList, current_index,
	 * recorder_index And pass, without change, to ReplyActivity.
	 */
	public void startReplyActivity(View view) {
		Log.d(TAG, "transition activity called");
		int added = this.getIntent().getExtras()
				.getInt("newPlayer");
		int recorder_index = this.getIntent().getExtras()
				.getInt("recorder_index");
		ArrayList<Integer> players = this.getIntent()
				.getExtras().getIntegerArrayList("player_arr");
		int current_index =this.getIntent().getExtras()
				.getInt("current_index");
		// Log.d(TAG, "number of players: "+ number_players);
		Log.d(TAG,"t "+recorder_index+" ci "+added+" afd "+current_index);
		intent.putExtras(TransitionActivity.this.getIntent().getExtras());
		if(added==1){
			Log.d(TAG,"t "+recorder_index+" ci ");
			intent.putExtra("current_index", recorder_index);
		}
		intent.putExtra("newPlayer", 0);
		startActivity(intent);
	}
	public void addAPlayer(View view) {
		Log.d(TAG, "newPlayer activity called");
		Intent temp_intent=TransitionActivity.this.getIntent();
		int recorder_index = this.getIntent().getExtras()
				.getInt("recorder_index");
		ArrayList<Integer> players = temp_intent
				.getExtras().getIntegerArrayList("player_arr");
		ArrayList<Integer> rounds_played = temp_intent
				.getExtras().getIntegerArrayList("rounds_played");
		int current_index = temp_intent.getExtras()
				.getInt("current_index");
		int added = temp_intent.getExtras()
				.getInt("newPlayer");
		Log.d(TAG,"t "+players.toString()+" ci "+(current_index-1));
		players.add(0);
		rounds_played.add(0);
		intent.putExtras(this.getIntent().getExtras());
		intent.putIntegerArrayListExtra("player_arr", players);
		intent.putIntegerArrayListExtra("rounds_played",rounds_played);
		intent.putExtra("current_index", players.size()-1);	
		intent.putExtra("newPlayer", 1);
		startActivity(intent);
	}
	
	public void retryRun(View view) {
		Log.d(TAG, "transition activity called");

		// Log.d(TAG, "number of players: "+ number_players);

		Intent intent = new Intent(TransitionActivity.this, ReplyActivity.class);
		intent.putExtras(this.getIntent().getExtras());
		ArrayList<Integer> players = this.getIntent()
				.getExtras().getIntegerArrayList("player_arr");
		int current_index = this.getIntent().getExtras()
				.getInt("current_index");
		int round = this.getIntent()
				.getExtras().getInt("num_rounds");
		if(current_index-1==-1){
			current_index=players.size();
		}
		ArrayList<Integer> rounds_played = this.getIntent()
				.getExtras().getIntegerArrayList("rounds_played");
		intent.putExtra("current_index", current_index-1);
		rounds_played.set(current_index-1, rounds_played.get(current_index-1)-1);
		
		intent.putIntegerArrayListExtra("player_arr", players);
		startActivity(intent);
	}
	
	public void endGame(View view) {
		

		// Log.d(TAG, "number of players: "+ number_players);
		intent = new Intent(TransitionActivity.this, ScoreActivity.class);
		intent.putExtras(TransitionActivity.this.getIntent().getExtras());
		intent.putExtra("endgame", true);
		Log.d(TAG, "transition activity called "+2049);
		startActivity(intent);
	}
	View.OnClickListener addNewPlayer = new View.OnClickListener() {
        public void onClick(View view) {   		
		Log.d(TAG, "newPlayer activity called");
		Intent temp_intent=TransitionActivity.this.getIntent();
		// Log.d(TAG, "number of players: "+ number_players);
		ArrayList<Integer> players = temp_intent
				.getExtras().getIntegerArrayList("player_arr");
		int current_index = temp_intent.getExtras()
				.getInt("current_index");
		int round = temp_intent
				.getExtras().getInt("num_rounds");
		int recorder_index =temp_intent.getExtras()
			.getInt("recorder_index");
		
		Log.d(TAG,"t "+players.toString()+" ci "+(current_index-1)+" r "+recorder_index);
		int time =temp_intent.getExtras()
				.getInt("time");
		ArrayList<String> recording =temp_intent
				.getExtras().getStringArrayList("recordingCoordinates");
		players.add(0);
		Intent intent = new Intent(TransitionActivity.this, ReplyActivity.class);
		intent.putExtra("time", time);
		intent.putStringArrayListExtra("recordingCoordinates", recording);
		intent.putExtra("current_index", players.size()-1);
		intent.putExtra("recorder_index", recorder_index);
		intent.putExtra("num_rounds", round);
		
		intent.putIntegerArrayListExtra("player_arr", players);
		startActivity(intent);
        }
	};
	/*
	 * startScoreActivity Get number_players, player ArrayList, current_index,
	 * recorder_index And pass, without change, to ReplyActivity.
	 */
//	public void endGame(View view) {
//		Log.d(TAG, "transition activity called");
//
//		// Log.d(TAG, "number of players: "+ number_players);
//		Intent intent = new Intent(TransitionActivity.this, ScoreActivity.class);
//		intent.putExtras(TransitionActivity.this.getIntent().getExtras());
//
//		startActivity(intent);
//	}
//	
	 View.OnClickListener scoreBoard = new View.OnClickListener() {
	        public void onClick(View view) {          	
	        	Log.d(TAG, "transition activity called");

	    		// Log.d(TAG, "number of players: "+ number_players);
	    		Intent intent = new Intent(TransitionActivity.this,ScoreActivity.class);
	    		intent.putExtras(TransitionActivity.this.getIntent().getExtras());
	    		intent.putExtra("endgame",false);
	    		startActivity(intent);
	          }
	      };
	
	/*
	 * startScoreActivity Get number_players, player ArrayList, current_index,
	 * recorder_index And pass, without change, to ReplyActivity.
	 */
	public void startScoreActivity(View view) {
	
		Log.d(TAG, "transition activity called");

		// Log.d(TAG, "number of players: "+ number_players);
		Intent intent = new Intent(TransitionActivity.this, ReplyActivity.class);
		intent.putExtras(TransitionActivity.this.getIntent().getExtras());

		startActivity(intent);
	}
	


    public void onBackPressed() {
    	Log.d(TAG, "on back pressed");
        Intent intent = new Intent(TransitionActivity.this, MainActivity.class);
        startActivity(intent);
    }
}
